﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("")]
    public partial class Unit : PageBase
    {
        private byte _modelId;
        private readonly UnitManage _myBLL = new UnitManage();

        private void Add()
        {
            var model = new WinnerCMS.Model.Unit();
            model.Name = this.txtName.Text.Trim();
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else if ((Request["ID"] != null) && (Request["Action"] == "Edit"))
            {
                model.Id = int.Parse(Request["ID"]);
            }
            string strErr = null;
            this._myBLL.Add(model, ref strErr);
            if (!string.IsNullOrEmpty(strErr))
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
            }
            else
            {
                MyJavascript.RegJS(this.UpdatePanel1, "window.location='Unit.aspx?ModelId=" + this._modelId + "';");
            }
        }

        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.GV_List.DataSource = this._myBLL.BindList(pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            if (this.GV_List.Rows.Count <= 0)
            {
                this.btnDelSeletedID.Enabled = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void Edit_Load()
        {
            var unit = this._myBLL.GetInfo(int.Parse(Request["ID"]));
            if (unit != null)
            {
                this.txtName.Text = unit.Name;
            }
            else
            {
                ErrClass.Redirect("数据不存在。");
            }
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var cell = e.Row.Cells[this.GV_List.Columns.Count - 1];
                if (cell.Controls.Count > 0)
                {
                    var button = (LinkButton) cell.Controls[0];
                    button.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBLL.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString(), this.UpdatePanel1);
            BindList(1);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this._modelId = byte.Parse(Request.QueryString["ModelId"]);
            if (!IsPostBack)
            {
                if ((Request["Action"] == null) && (Request["ID"] == null))
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Unit.aspx?ModelId={1}'>单位管理</a>",
                        ModelManage.GetModelName(this._modelId), this._modelId);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    BindList(1);
                }
                else if ((Request["ID"] != null) && (Request["Action"] == "Eidt"))
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Unit.aspx?ModelId={1}'>单位管理</a> → 修改",
                        ModelManage.GetModelName(this._modelId), this._modelId);
                    this.btnOK.Text = "修改";
                    Edit_Load();
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
                else if ((Request["ID"] == null) && (Request["Action"] == "Add"))
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Unit.aspx?ModelId={1}'>单位管理</a> → 添加",
                        ModelManage.GetModelName(this._modelId), this._modelId);
                    this.btnOK.Text = "添加";
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
            }
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}